package com.ang.utils;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.HashSet;
import java.util.Set;

/**
 * @author ssang
 * @date 2023-03-16 16:26
 */
public class CommonUtil {

    /**
     * 获取object值为null的属性
     */
    public static String[] getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNameSet = new HashSet<>();
        for(PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            // 此处判断可根据需求修改
            if (srcValue == null) {
                emptyNameSet.add(pd.getName());
            }
        }
        String[] result = new String[emptyNameSet.size()];
        return emptyNameSet.toArray(result);
    }
}
